package app.base.systemmanage.systemusertypemanage;

import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import app.base.common.Utility;

import app.base.systemmanage.systemusertypemanage.dao.SysUserTypeDAO;
import app.base.systemmanage.systemusertypemanage.model.SysUserType;

public class SystemUserTypeLayerQuery extends HttpServlet{

	private static final long serialVersionUID = 1L;
	
	public void doGet(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
	    request.setCharacterEncoding(Utility.CHARSET_ENCODING_UTF8);
	    response.setContentType(Utility.DEFAULT_CONTENT_TYPE);

		PrintWriter out = response.getWriter(); 
		
		String node = request.getParameter("node");
		String module = request.getParameter("module");	
		
	    if (!(node == null || node.trim().length() == 0))
	    {
		    node = URLDecoder.decode(node, Utility.CHARSET_ENCODING_UTF8).trim();
		    
			if (!(module == null))
			{
				module = URLDecoder.decode(module, Utility.CHARSET_ENCODING_UTF8).trim();
				node = node.substring(0, node.length() - module.length());
			}
	    	
		    Map<String, Object> map = new HashMap<String, Object>();
		    
			map.put("USER_TYPE_PARENT_ID", node);
			map.put("module", module);
			
		    out.println(getTreeSubNodesByNodeId(map).toString());
	    }
		
	}
	
	public void doPost(HttpServletRequest request, HttpServletResponse response) 
		throws ServletException, IOException 
	{
		doGet(request, response);
	}
	
	//return JSON format.
	private StringBuffer getTreeSubNodesByNodeId(Map<String, Object> map)
	{
		StringBuffer json = new StringBuffer("[");
		
		ApplicationContext context = new ClassPathXmlApplicationContext(Utility.DEFAULT_SPRINGFRAMEWORK_APPLICATION_CONTEXT_FILE_PATH);
		SysUserTypeDAO sysUserTypeDAO = (SysUserTypeDAO)context.getBean(Utility.SYSTEM_USER_TYPE_MANAGE_MODULE_BEAN_NAME);
		
		List<SysUserType> list = sysUserTypeDAO.select_0002(map);
			
		for(int index = 0; index < list.size(); index++)
		{
			SysUserType sysUserType = (SysUserType) list.get(index);
			
			String userTypeId = (map.get("module") == null) ? sysUserType.getUSER_TYPE_ID().trim() : sysUserType.getUSER_TYPE_ID().trim() + map.get("module").toString();
			String userTypeParentId = sysUserType.getUSER_TYPE_PARENT_ID().trim();
			String userTypeName = sysUserType.getUSER_TYPE_NAME().trim();
			String userTypeDesc = (sysUserType.getUSER_TYPE_DESC() == null) ? "" : sysUserType.getUSER_TYPE_DESC().trim();
			String normalIcon = sysUserType.getNORMAL_IMG_URL().trim();
			String expandIcon = sysUserType.getEXPAND_IMG_URL().trim();

			json.append("{id: '" + userTypeId + "',");
			json.append("userTypeParentId: '" + userTypeParentId + "',");
			json.append("text: '" + userTypeName + "',");
			json.append("qtip: '" + userTypeDesc + "',");
			json.append("icon: '" + normalIcon + "',");
			json.append("expandIcon: '" + expandIcon + "',");
			json.append("singleClickExpand: true");

			json.append("}");
			
			if (index < list.size() - 1)
			{
				json.append(", ");
			}
		}
		
		json.append("]");
		
		return json;
	}
	
	/**
	 * @param args
	 */
	public static void main(String[] args) {
		// TODO Auto-generated method stub

	}

}